package com.housesales.web;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import module.BasicBanKuai;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import bussinessService.basicbussinessService;
import bussinessService.impl.BasicBanKuaiBussinessServiceImpl;
import domain.vo.BlockQueryObject;
import domain.vo.BlockVO;
import domain.vo.JSONObject;
import domain.vo.Message;

/**
 * 
* @ClassName: BlockController
* @Description: 板块CONTROLLER
* @author chenchao
* @date 2013-12-12 上午07:33:44
*
 */
@Controller
@RequestMapping("/block")
public class BlockController {
	private Logger logger = Logger.getLogger(BlockController.class.getName());
	/**
	 * 板块业务层实现类
	 */
	private basicbussinessService<BasicBanKuai,BlockVO>  blockBSI = new BasicBanKuaiBussinessServiceImpl();
	
	@RequestMapping("/getBlocksJson")
	public @ResponseBody JSONObject<BlockVO> getBlocksJson(@RequestParam("districtId") int districtId) {
		JSONObject<BlockVO> jsonObject = new JSONObject<BlockVO>();
		BlockQueryObject bQO = new BlockQueryObject();
		bQO.setDistrictId(districtId);
		try {
			List<BlockVO> tempVOS = new ArrayList<BlockVO>(); 
			tempVOS = blockBSI.getObjectByQueryObject(bQO);
			jsonObject.setTotal(tempVOS.size());
			jsonObject.setRows(tempVOS);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	
	@RequestMapping("/getTreeBlocksJson")
	public @ResponseBody JSONObject<BlockVO> getTreeBlocksJson(@RequestParam("id") int districtId) {
		JSONObject<BlockVO> jsonObject = new JSONObject<BlockVO>();
		BlockQueryObject bQO = new BlockQueryObject();
		bQO.setDistrictId(districtId);
		try {
			List<BlockVO> tempVOS = new ArrayList<BlockVO>(); 
			tempVOS = blockBSI.getObjectByQueryObject(bQO);
			tempVOS.remove(0);
			jsonObject.setTotal(tempVOS.size());
			jsonObject.setRows(tempVOS);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return jsonObject;
	}
	
	
	
	@RequestMapping("/addBlock")
	public @ResponseBody  Message saveBlock(@RequestParam("bhaid") int bhaid,@RequestParam("blockName") String bolckName ) {
		Message ms = new Message();
		try {
			BasicBanKuai bbk = new BasicBanKuai();
			bbk.setBb_name(bolckName);
			bbk.setBha_id(bhaid);
			blockBSI.merageObject(bbk);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			ms.setErrorMsg("数据保存失败");
		}
		return ms;
	}
}
